package org.ws.task.netty.handler;

import com.google.common.collect.Maps;
import org.ws.task.netty.RequestActionHandler;
import org.ws.task.model.protocol.base.RequestAction;

import java.util.Map;

public class DefaultActionHandlerRegistry implements ActionHandlerRegistry {

    private Map<Integer, RequestActionHandler> actionHandlerMap = Maps.newHashMap();

    @Override
    public void register(RequestActionHandler handler) {
        if(!actionHandlerMap.containsKey(handler.action())){
            actionHandlerMap.put(handler.action().getCode(),handler);
        }
    }

    @Override
    public RequestActionHandler get(RequestAction action) {
        if(actionHandlerMap.containsKey(action.getCode())){
            return actionHandlerMap.get(action.getCode());
        }
        return null;
    }

    @Override
    public void clear() {
        this.actionHandlerMap.clear();
    }
}
